package com.study_online.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study_online.interceptor.LoginInterceptor;
import com.study_online.annotation.UpdateTimeAnnotation;
import com.study_online.domain.*;
import com.study_online.mapper.*;
import com.study_online.service.ForumService;
import com.study_online.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 张天霸
 * <p>
 * 帖子实现类
 */
@Service
public class ForumServiceImpl implements ForumService {

    @Autowired
    private ForumFatherPostMapper forumFatherPostMapper;

    @Autowired
    private MyBaseMapper myBaseMapper;

    @Autowired
    private ForumChildPostMapper forumChildPostMapper;

    @Autowired
    private ForumCommentsMapper forumCommentsMapper;

    @Autowired
    private ForumReplyMapper forumReplyMapper;


    /**
     * 发表帖子
     *
     * @param forumFatherPost
     * @return
     */
    @Override
    public ResultInfo publish(ForumFatherPost forumFatherPost) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            //获取当前时间
            String addTime = TimeUtils.getTime();
            //获取用户Id
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            //设置用Id
            forumFatherPost.setUid(uid);
            //设置创建时间
            forumFatherPost.setAddTime(addTime);
            forumFatherPost.insert();
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("发表失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 查看帖子（分页，模糊查询）
     *
     * @param pageNum
     * @param pageSize
     * @param forumFatherPost
     * @return
     */
    @Override
    public ResultInfo selectForumFatherPostByCondition(Integer pageNum, Integer pageSize, ForumFatherPost forumFatherPost) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            //配置分页
            Page<ForumFatherPost> page = new Page<>(pageNum, pageSize);
            //配置条件筛选器
            QueryWrapper<ForumFatherPost> wrapper = new QueryWrapper<>();
            wrapper.like("fatherpost_title", forumFatherPost.getFatherpostTitle());
            IPage<ForumFatherPost> iPage = forumFatherPostMapper.selectPage(page, wrapper);
            //得到表中信息
            List<ForumFatherPost> records = iPage.getRecords();
            info.setData(records);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 收藏帖子
     *
     * @param collectedId
     * @return
     */
    @Override
    public ResultInfo collect(Integer collectedId) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            myBaseMapper.collect(collectedId, uid, TimeUtils.getTime());
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("收藏失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 取消收藏
     *
     * @param collectedId
     * @return
     */
    @Override
    public ResultInfo cancelCollect(Integer collectedId) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            myBaseMapper.deleteCollect(collectedId, uid);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("取消收藏失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 修改帖子
     *
     * @param forumFatherPost
     * @return
     */
    @Override
    @UpdateTimeAnnotation(Id = "#{forumFatherPost.fatherpostId}", tablename = "forum_fatherpost", IdName = "fatherpostId")
    public ResultInfo update(ForumFatherPost forumFatherPost) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            forumFatherPost.setUid(uid);
            forumFatherPost.updateById();
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("修改失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 发表回帖
     *
     * @param forumChildPost
     * @return
     */
    @Override
    public ResultInfo replyPost(ForumChildPost forumChildPost) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            forumChildPost.setUid(uid);
            forumChildPost.setAddTime(TimeUtils.getTime());
            forumChildPost.insert();
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("回帖失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 查看回帖（分页）
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultInfo selectForumChildPost(Integer pageNum, Integer pageSize) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            Page<ForumChildPost> page = new Page<>();
            IPage<ForumChildPost> iPage = forumChildPostMapper.selectPage(page, null);
            List<ForumChildPost> list = iPage.getRecords();
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 评论回帖
     *
     * @param forumComments
     * @return
     */
    @Override
    public ResultInfo comment(ForumComments forumComments) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            forumComments.setUid(uid);
            forumComments.setAddTime(TimeUtils.getTime());
            forumComments.insert();
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("评论回帖失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 点赞
     *
     * @param likeId
     * @param likeType
     * @return
     */
    @Override
    public ResultInfo like(Integer likeId, String likeType) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            String tableName = "";
            switch (likeType) {
                case "child":
                    tableName = "childpost_like";
                    break;
                case "father":
                    tableName = "fatherpost_like";
                    break;
                case "comment":
                    tableName = "comments_like";
                    break;
                case "reply":
                    tableName = "reply_like";
                    break;
            }

            myBaseMapper.insertLike(likeId, uid, TimeUtils.getTime(), tableName);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("点赞失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 取消点赞
     *
     * @param likeId
     * @param likeType
     * @return
     */
    @Override
    public ResultInfo cancel(Integer likeId, String likeType) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            String tableName = "";
            String tableField = "";
            switch (likeType) {
                case "child":
                    tableName = "childpost_like";
                    tableField = "childpost_id";
                    break;
                case "father":
                    tableName = "fatherpost_like";
                    tableField = "fatherpost_id";
                    break;
                case "comment":
                    tableName = "comments_like";
                    tableField = "comment_id";
                    break;
                case "reply":
                    tableName = "reply_like";
                    tableField = "reply_id";
                    break;
            }
            myBaseMapper.deleteLike(likeId, uid, TimeUtils.getTime(), tableName, tableField);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("取消点赞失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 点踩
     *
     * @param dislikeId
     * @param dislikeType
     * @return
     */
    @Override
    public ResultInfo dislike(Integer dislikeId, String dislikeType) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            String tableName = "";
            switch (dislikeType) {
                case "child":
                    tableName = "childpost_dislike";
                    break;
                case "father":
                    tableName = "fatherpost_dislike";
                    break;
                case "comment":
                    tableName = "comments_dislike";
                    break;
                case "reply":
                    tableName = "reply_dislike";
                    break;
            }
            myBaseMapper.insertDislike(dislikeId, uid, TimeUtils.getTime(), tableName);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("点踩失败！");
            e.printStackTrace();
        }
        return info;
    }

    @Override
    public ResultInfo cancelDislike(Integer dislikeId, String dislikeType) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            String tableName = "";
            String tableField = "";
            switch (dislikeType) {
                case "child":
                    tableName = "childpost_dislike";
                    tableField = "childpost_id";
                    break;
                case "father":
                    tableName = "fatherpost_dislike";
                    tableField = "fatherpost_id";
                    break;
                case "comment":
                    tableName = "comments_dislike";
                    tableField = "comment_id";
                    break;
                case "reply":
                    tableName = "reply_dislike";
                    tableField = "reply_id";
                    break;
            }
            myBaseMapper.deleteDislike(dislikeId, uid, TimeUtils.getTime(), tableName, tableField);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("取消点踩失败！");
            e.printStackTrace();
        }
        return info;
    }


    /**
     * 查看评论（分页）
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultInfo selectComment(Integer pageNum, Integer pageSize) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            Page<ForumComments> page = new Page<>(pageNum, pageSize);
            IPage<ForumComments> iPage = forumCommentsMapper.selectPage(page, null);
            List<ForumComments> list = iPage.getRecords();
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败！");
            e.printStackTrace();
        }
        return info;
    }


    /**
     * 回复评论
     *
     * @param forumReply
     * @return
     */
    @Override
    public ResultInfo reply(ForumReply forumReply) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            forumReply.setUid(uid);
            forumReply.setAddTime(TimeUtils.getTime());
            forumReply.insert();
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("回复失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 查看回复（分页）
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultInfo selectReply(Integer pageNum, Integer pageSize) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            Page<ForumReply> page = new Page<>(pageNum, pageSize);
            IPage<ForumReply> iPage = forumReplyMapper.selectPage(page, null);
            List<ForumReply> list = iPage.getRecords();
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 删除回复
     *
     * @param replyId
     * @return
     */
    @Override
    public ResultInfo deleteReply(Integer replyId) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            myBaseMapper.deleteById(replyId);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("删除失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 删除帖子（包含删除所有连带回帖、评论、收藏、点赞、点踩）
     * @param fpId
     * @return
     */
    @Override
    public ResultInfo deleteForum(Integer fpId) {
        ResultInfo info = new ResultInfo();
        try {
            User user = LoginInterceptor.getUser();
            Integer uid = user.getUid();
            myBaseMapper.deleteForum(fpId);
            myBaseMapper.deleteCollect(fpId, uid);
            info.setFlag(true);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("删除帖子失败！");
            e.printStackTrace();
        }
        return info;
    }

    @Override
    public ResultInfo findForumByPostTypeId(int postTypeId) {
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            List<ForumFatherPost> postAll = forumFatherPostMapper.findForumByPostTypeId(postTypeId);
            info.setData(postAll);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查询专区所有帖子失败！");
            e.printStackTrace();
        }

        return info;
    }

    @Override
    public ResultInfo findAllForum(Integer pageNum, Integer pageSize) {
        ResultInfo info=new ResultInfo();

        info.setFlag(true);
        //分页显示
        System.out.println(pageNum+" : "+pageSize);
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<ForumFatherPost> posts=forumFatherPostMapper.findAllForum();
            for (ForumFatherPost post : posts) {
                System.out.println(post);
            }
            PageInfo<ForumFatherPost> searchPageInfo = null;
            searchPageInfo = new PageInfo<ForumFatherPost>(posts);
            System.out.println(searchPageInfo.getPageNum());
            System.out.println(searchPageInfo.getPageSize());
            System.out.println(searchPageInfo.getNextPage());
            info.setData(posts);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查询所有帖子失败！");
            e.printStackTrace();
        }
        return info;
    }


}